<!DOCTYPE html>
<html lang="en">
<head>
		<title>Snippets &mdash; Create dictionary with multiple values</title>
		<meta charset="utf-8" />
		<link rel="profile" href="http://gmpg.org/xfn/11" />
		<link rel="stylesheet" type="text/css" href="https://corteousquestions.club/theme/css/style.css" />
		<link rel='stylesheet' id='oswald-css'  href='http://fonts.googleapis.com/css?family=Oswald&#038;ver=3.3.2' type='text/css' media='all' />
		<style type="text/css">
			body.custom-background { background-color: #f5f5f5; }
		</style>
		<link rel="alternate" type="application/atom+xml"
			title="Snippets — Flux Atom"
			href="https://corteousquestions.club/" /> 
		<!--[if lte IE 8]><script src="https://corteousquestions.club/theme/js/html5shiv.js"></script><![endif]-->
</head>

<body class="home blog custom-background " >
	<div id="container">
		<div id="header">
				<h1 id="site-title"><a href="https://corteousquestions.club">Snippets</a></h1>
<h2 id="site-description">All that I will forget is here</h2>		</div><!-- /#banner -->
		
		<div id="menu">
			<div class="menu-navigation-container">
				<ul id="menu-navigation" class="menu">
						<li class="active" class="menu-item menu-item-type-post_type menu-item-object-page"><a href="https://corteousquestions.club/category/misc.html">misc</a></li>

				</ul>
			</div> <!--/#menu-navigation-container-->
		</div><!-- /#menu -->
		
		<div class="page-title">
		</div>
	
		<div id="contents">

<div class="post type-post status-publish format-standard hentry category-general" id="post">
	<div class="entry-meta">
		<div class="date"><a href="https://corteousquestions.club/create-dictionary-multiple-values.html">Sun 21 April 2019</a></div>
		<span class="byline">By <a href="https://corteousquestions.club/author/mclds.html">mclds</a></span>
			<span class="cat-links"><a href="https://corteousquestions.club/category/misc.html" title="View all posts in misc" rel="category tag">misc</a></span>
	</div> <!-- /#entry-meta -->
	<div class="main">
		<h2 class="entry-title">
			<a href="https://corteousquestions.club/create-dictionary-multiple-values.html" title="Permalink to Create dictionary with multiple values" rel="bookmark">Create dictionary with multiple values</a>
		</h2>
		<div class="entry-content">
			<p>There are a couple of ways to add values to key, and to create a list
 if one isn't already there. I'll show one such method in little steps.</p>
<hr>
<p>key = somekey
 a.setdefault(key, [])
 a[key].append(1)</p>
<hr>
<p>Results:</p>
<hr>
<blockquote>
<blockquote>
<blockquote>
<p>a
 {'somekey': [1]}
 Next, try:
 key ='somekey'
 a.setdefault(key, [])
 a[key].append(2)</p>
</blockquote>
</blockquote>
</blockquote>
<hr>
<p>Results:</p>
<hr>
<blockquote>
<blockquote>
<blockquote>
<p 2_="2]" _1_="[1," _somekey_:="'somekey':">a</p>
</blockquote>
</blockquote>
</blockquote>
<hr>
<p>The magic of setdefault is that it initializes the value for that key
 if that key is not defined, otherwise it does nothing. Now, noting
 that setdefault returns the key you can combine these into a single line:</p>
<hr>
<p>a.setdefault('somekey',[]).append('bob')</p>
<hr>
<p>Results:</p>
<hr>
<blockquote>
<blockquote>
<blockquote>
<p 2_="2," _1_="[1," _bob_="'bob']" _somekey_:="'somekey':">a</p>
</blockquote>
</blockquote>
</blockquote>
<hr>
<p>https://bit.ly/3p3h2O4</p>
		</div> <!--/#entry-content-->
    		<span class="tag-links"><strong>Tagged</strong>
 <a href="https://corteousquestions.club/tag/dictionary.html" rel="tag">dictionary</a>,  <a href="https://corteousquestions.club/tag/key.html" rel="tag">key</a>,  <a href="https://corteousquestions.club/tag/python.html" rel="tag">python</a>    		</span>
	</div> <!--/#main-->
</div>  <!--/#post-->

		</div>
		
		<div id="footer">
			<p>Powered by <a href="http://getpelican.com">Pelican</a>, theme by <a href="http://bunnyman.info">tBunnyMan</a>.</p>
		</div><!-- /#footer -->
	</div><!-- /#container -->
	<div style="display:none"></div>
</body>
</html>